<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/10/23 0023
 * Time: 14:59
 */
namespace app\modules\api\controllers;

use app\hejiang\BaseApiResponse;
use app\models\Shop;
use app\models\Order;
use app\models\User;
use app\models\Cash;
use app\models\UserShareMoney;
use yii\data\Pagination;
use app\modules\api\models\TeamForm;
use app\models\WechatTplMsgSender;
use app\models\WechatApp;
use luweiss\wechat\Wechat;

class TeamController extends Controller
{


    public function actionIndex()
    {
        $data = [];

        $user = User::findOne(['id' => \Yii::$app->user->id]);

        $shop = Shop::findOne(['user_id' => \Yii::$app->user->id]);

        $start_time= mktime(0,0,0,date('m'),1,date('Y'));
        $end_time = mktime(23,59,59,date('m'),date('t'),date('Y'));

        $data['team']['avatar_url'] = $user->avatar_url;
        $data['team']['id'] = $shop->id;
        $data['team']['address'] = $shop->address;

        //可提现金额
        $data['team_price'] = $user->team_price;

        //累计提现金额
        $data['total_cash'] = Cash::find()->where(['user_id'=>\Yii::$app->user->identity->id,'status'=>2,'flag'=>1])->SUM("price");
        $data['total_cash']  = ! $data['total_cash'] ?'0.00': $data['total_cash'] ;


        //总佣金
        $data['total_team_price'] = $user->total_team_price;

        //本月佣金
        $data['month_team_price'] = UserShareMoney::find()->where(['user_id'=>\Yii::$app->user->identity->id,"flag"=>1])->andWhere(['>=','addtime',$start_time])->andWhere(['<=','addtime',$end_time])->SUM("money");
        $data['month_team_price']  = bcadd($data['month_team_price'],0,2) ;

        
        //总订单量
        $data['order_count'] = Order::find()->where(['is_pay'=>1,'is_delete'=>0,'apply_delete'=>0,'shop_id'=>$shop->id])->count();

        //本月订单量
        $data['month_order_count'] = Order::find()->where(['is_pay'=>1,'is_delete'=>0,'apply_delete'=>0,'shop_id'=>$shop->id])
            ->andWhere(['>=','pay_time',$start_time])->andWhere(['<=','pay_time',$end_time])->count();


        //总销售额
        $data['order_money'] = Order::find()->where(['is_pay'=>1,'is_delete'=>0,'apply_delete'=>0,'shop_id'=>$shop->id])->SUM("pay_price");
        $data['order_money']  = ! $data['order_money'] ?'0.00': $data['order_money'] ;
        $data['order_money']  = bcadd($data['order_money'],0,2) ;


        //本月销售额
        $data['month_order_money'] = Order::find()->where(['is_pay'=>1,'is_delete'=>0,'apply_delete'=>0,'shop_id'=>$shop->id])
            ->andWhere(['>=','pay_time',$start_time])->andWhere(['<=','pay_time',$end_time])->SUM("pay_price");
        $data['month_order_money']  = ! $data['month_order_money'] ?'0.00': $data['month_order_money'] ;
        $data['month_order_money']  = bcadd($data['month_order_money'],0,2) ;


        return new BaseApiResponse(['code'=>0,'msg'=>"操作成功",'data'=>$data]);

    }


    public function actionAccountLog()
    {
        $teamForm = new TeamForm();
        $teamForm->user_id = \Yii::$app->user->id;
        $teamForm->page = \Yii::$app->request->get('page',1);
        return new BaseApiResponse($teamForm->accountLog());

    }

    public function actionSendMsg()
    {
        $data = [];

        $shop = Shop::findOne(['user_id'=>\Yii::$app->user->id]);
        $shop_id = $shop->id;
        if(!$shop_id) return new BaseApiResponse(['code'=>1,'msg'=>"不是团长",'data'=>""]);

        $wechat_app = WechatApp::findOne(2);
        if (!$wechat_app) {
            return;
        }

        $wechat = new Wechat([
            'appId' => $wechat_app->app_id,
            'appSecret' => $wechat_app->app_secret,
            'mchId' => $wechat_app->mch_id,
            'apiKey' => $wechat_app->key,
            'cachePath' => \Yii::$app->runtimePath . '/cache',
        ]);

        $list = Order::find()->where(['is_pay'=>1,'is_send'=>1,'is_delete'=>0,'shop_id'=>$shop_id,'apply_delete'=>0,'is_notice'=>0])->all();
        foreach ($list as  $order) {
            $wechat_tpl_meg_sender = new WechatTplMsgSender(2, $order->id, $wechat);
            $wechat_tpl_meg_sender->payMsg();

            $order->is_notice = 1;
            $order->save();

        }
        return new BaseApiResponse(['code'=>0,'msg'=>"操作成功",'data'=>$data]);
    }

}
